<?php

namespace App\Http\Controllers\Admin;

use App\Http\Response;
use App\Order;
use App\OrderDetail;
use App\Sku;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class OrderDetailController extends Controller
{
    use Response;
    public function index($order_id,Request $request)
    {
        $data=Order::with('detail')->where('order_number',$order_id)->first();
        return $this->success(200,$data);
    }

    public function update($id,Request $request)
    {
        $data=$request->all();
        if (isset($data['sku_id'])){
            $sku=Sku::find($data['sku_id']);
            $data['sku']=$sku['spec_text'];
            $detail=OrderDetail::find($id);
            $order=Order::where('order_number',$detail->order_id)->first();
            if ($order->status==1){
                Sku::where('id',$detail->sku_id)->decrement("number",$detail->number);
                Sku::where('id',$data['sku_id'])->increment("number",$detail->number);
            }
        }
        $update=OrderDetail::where('id',$id)->update($data);

        return $this->message(200,'修改成功');
    }
}
